/**
 * @Author Xin Xiao
 * @Description
 * @Version 1.0.0
 * @Date 2021/3/03 16:23 下午
 * @Copyright MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
 */

 var TLvComponentBase = require("./TlvComponentBase").TLvComponentBase
 var Block = require("../encoding/Block")
 var Encoder = require("../encoding/Encoder")
 var ComponentType = require("../encoding/TLV").ComponentType
 var SelfEncodingBase = require('../encoding/SelfEncodingBase').SelfEncodingBase
 var TLV = require("../encoding/TLV")
 var TlvComponentBase = require('./TlvComponentBase').TlvComponentBase
 var aggregation = require('../common/aggregation')//实现了多继承
 var buffer2arraybuffer = require('../common/buffer2arraybuffer')


 /**
  * @description 表示一个签名值 TLV
  * 1.格式如下：
  * SignatureValue = 200 TLV-LENGTH *OCTET
  * 2.详情参见：http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
  */
 class SignatureValue extends  aggregation(TlvComponentBase,SelfEncodingBase) {
  constructor(){
    super()
    this.value = new ArrayBuffer()
  }

  /**
   * @description 根据签名值字节数组初始化一个 SignatureValue
   * @param {Uint8Array} value
   */
  buildSignatureValueByByteArray(value){
    try {
      if(value instanceof Uint8Array){
        value = value.buffer //将传输过来的字节数组复制给value
      }
      else{
        if(value instanceof Buffer)
        value = buffer2arraybuffer(value)
      }
      this.value = value
    } catch (error) {
      throw error
    }
  }

  /**
   * @description 根据 TLV Block 初始化一个 SignatureValue
   * @param {Block} block
   */
  buildSignatureValueByBlock(block){
    try {
      this.wireDecode(block)
    } catch (error) {
      throw error
    }
  }

  /**
   * @description 得到签名值的字节数组
   * @return {Uint8Array} value
   */
  getValue(){
    return this.value
  }

  /**
	 * @description 将 SignatureValue 线速编码为一个 TLV
	 * @param {Encoder} encoder
	 * @return {number} totalLength
	 * @throws
	 */
	wireEncode(encoder){
		let totalLength = 0
		try {
			//编码 TLV-VALUE
			let tmpLen = 0
      if(this.value instanceof Buffer) this.value = buffer2arraybuffer(this.value)
      tmpLen = encoder.prependByteArray(buffer2arraybuffer(this.value), BigInt(buffer2arraybuffer(this.value).byteLength))
			totalLength += tmpLen

			//编码 TLV-LENGTH
			tmpLen = encoder.prependVarNumber(BigInt(totalLength))
			totalLength += tmpLen

			//编码 TLV-TYPE
			tmpLen = encoder.prependVarNumber(ComponentType.TlvSignatureValue)
			totalLength += tmpLen

			return totalLength
		} catch (error) {
			throw error
		}
	}

	/**
	 * @description 从 TLV Block 中解码出一个SignatureValue
	 * @param {Block} block
	 */
	wireDecode(block){
		try {
			//检查Type是否正确
			TLV.expectType(block.getType(),ComponentType.TlvSignatureValue)
      var value = block.getValue()
      if(value instanceof Uint8Array){
        value = value.buffer //将传输过来的字节数组复制给value
      }
      else{
        if(value instanceof Buffer)
        value = buffer2arraybuffer(value)
      }
      this.value = value

		} catch (error) {
			throw error
		}
	}
 }

 /**
  * @description 根据签名值字节数组创建一个 SignatureValue
  * @param {Uint8Array} value
  * @return {SignatureValue}
  */
 function createSignatureValueByByteArray(value){
  var signatureValue = new SignatureValue()
  try {
    signatureValue.buildSignatureValueByByteArray(value)
    return signatureValue
  } catch (error) {
    throw error
  }
 }

  /**
  * @description 根据 TLV Block 创建一个 SignatureValue
  * @param {Block} block
  * @return {SignatureValue}
  */
 function createSignatureValueByBlock(block){
  var signatureValue = new SignatureValue()
  try {
    signatureValue.buildSignatureValueByBlock(block)
    return signatureValue
  } catch (error) {
    throw error
  }
 }

 module.exports = {
  SignatureValue: SignatureValue,
  createSignatureValueByBlock: createSignatureValueByBlock,
  createSignatureValueByByteArray: createSignatureValueByByteArray
 }